package utils

import (
	"github.com/gin-gonic/gin"
	"strings"
)

// IpAddr 获取客户端真实IP地址， eg: 127.0.0.1
func IpAddr(ctx *gin.Context) string {
	xForwardedFor := ctx.GetHeader("X-Forwarded-For")
	var clientIP string
	if xForwardedFor != "" {
		clientIP = xForwardedFor
	} else if ip := ctx.GetHeader("X-Real-Ip"); ip != "" {
		clientIP = ip
	} else {
		hostPort := strings.Split(ctx.RemoteIP(), ":")
		if len(hostPort) > 0 {
			clientIP = hostPort[0]
		}
	}
	return clientIP
}
